package cn.lgs.com.mobileplayer.view;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.common.collect.Lists;

import java.lang.ref.WeakReference;
import java.util.List;

import butterknife.BindView;
import cn.lgs.com.mobileplayer.R;
import cn.lgs.com.mobileplayer.base.BaseFragment;
import cn.lgs.com.mobileplayer.entity.MediaItemBean;
import cn.lgs.com.mobileplayer.utils.LogUtil;
import cn.lgs.com.mobileplayer.utils.UIHandler;

/**
 * Created by lgs on 2018/2/19.
 * 本地视频界面
 */

public class LocationVideoFragment extends BaseFragment {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @BindView(R.id.pb_loading)
    ProgressBar pbLoading;
    private ContentResolver resolver;
    private List<MediaItemBean> mediaList;

    private UIHandler mHandler = new UIHandler(this) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //            Fragment fragment = handler.getSoftReference().get();
            //            if (null != fragment) {
            //            }
            if (null != mediaList && !mediaList.isEmpty()) {
                //有数据设置适配器,并且把文本和ProgressBar
                LogUtil.i("mediaList大小：" + mediaList.size());
            } else {
                //没有数据显示空信息

            }
            //ProgressBar隐藏
            pbLoading.setVisibility(View.GONE);
        }
    };

    //    private Handler mHandler = new Handler() {
    //        @Override
    //        public void handleMessage(Message msg) {
    //            if (null != mediaList && !mediaList.isEmpty()) {
    //                //有数据设置适配器,并且把文本和ProgressBar隐藏
    //            } else {
    //                //没有数据显示空信息
    //
    //            }
    //            //ProgressBar隐藏
    //        }
    //    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_location_video;
    }

    @Override
    protected void init() {
        resolver = getContext().getContentResolver();
        mediaList = Lists.newArrayList();
        //加载本地视频数据
        getDataFromLocal();
    }

    /**
     * 从本地的sdcard得到数据
     * 1、遍历sdcard，后缀名
     * 2、从内容提供者里面获取视频
     * 3、如果是6.0系统，动态获取sdcard的权限
     */
    private void getDataFromLocal() {

        MyThread thread = new MyThread();
        thread.start();
    }

    private class MyThread extends Thread {
        //content://media/external/video/media
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        //        Uri uri_ = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String[] objs = {
                MediaStore.Video.Media.DISPLAY_NAME,//文件在sdcard中的名称
                MediaStore.Video.Media.DURATION,//持续时间
                MediaStore.Video.Media.SIZE,//视频文件大小
                MediaStore.Video.Media.DATA,//视频绝对地址
                MediaStore.Video.Media.ARTIST,//

                //                MediaStore.Audio.Media.DISPLAY_NAME,
                //                MediaStore.Audio.Media.DURATION,
                //                MediaStore.Audio.Media.SIZE,
                //                MediaStore.Video.Media.DATA,
                //                MediaStore.Audio.Media.ARTIST
        };

        @Override
        public void run() {
            super.run();
            Cursor cursor = resolver.query(uri, objs, null, null, null);
            if (null != cursor) {
                while (cursor.moveToNext()) {
                    String name = cursor.getString(0);  //文件名称
                    long duration = cursor.getLong(1);  //视频时间
                    long size = cursor.getLong(2);  //大小
                    String data = cursor.getString(3);//绝对地址
                    String artist = cursor.getString(4);//音乐

                    mediaList.add(new MediaItemBean(name, duration, size, data, artist));
                }
                cursor.close();
            }
            //发消息
            mHandler.sendEmptyMessage(0);//当handler有多种情况要处理时才区分
        }
    }

    private class HandlerUtils extends Handler {

        private WeakReference<Fragment> weakReference;

        public HandlerUtils(Fragment fragment) {
            this.weakReference = new WeakReference<>(fragment);
        }
    }
}
